package com.naiterui.ehp.bs.im.network.transcode.coders;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.naiterui.ehp.bs.im.exception.NaiteruiIMException;
import com.naiterui.ehp.bs.im.network.protocl.Message;
import com.naiterui.ehp.bs.im.network.protocl.RecommandDrug;

/**
 * 负责推荐用药消息的编码和解码
 * <P>File name : DrugRecTransCoder.java </P>
 * <P>Author : zhouyanxin </P>
 * <P>Date : 2015年6月22日 </P>
 */
@Service
public class DrugRecTransCoder extends AbstractTransCoder<RecommandDrug> {

    /**
     * 推荐用药消息解码
     * <P>Author : zhouyanxin </P>
     * <P>Date : 2015年6月22日 </P>
     * @param message
     * @return
     * @throws NaiteruiIMException
     * @see com.naiterui.ehp.bs.im.network.transcode.coders.AbstractTransCoder#toMessageObject(java.lang.String)
     */
    @Override
    protected RecommandDrug deserializeMessageContent(String content) throws NaiteruiIMException {
        RecommandDrug recommandDrug = JSON.parseObject(content, RecommandDrug.class);
        if (null == recommandDrug.getRecommandStatus()) {
            recommandDrug.setRecommandStatus(recommandDrug.getCheckingStatus());
            recommandDrug.setCheckingStatus(null);
        }
        return recommandDrug;
    }

    @Override
    public String serializeMessage(Message<?> message) throws NaiteruiIMException {
        RecommandDrug recommandDrug = (RecommandDrug) message.getContent();
        if (null == recommandDrug.getCheckingStatus()) {
            recommandDrug.setCheckingStatus(recommandDrug.getRecommandStatus());
            recommandDrug.setRecommandStatus(null);
        }
        return super.serializeMessage(message);
    }

}
